package com.heima.wemedia.filter;

import com.heima.model.media.pojos.WmUser;
import com.heima.search.utils.threadlocal.WmThreadLocalUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.core.annotation.Order;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Slf4j
@Order(0)
@WebFilter(filterName = "wmTokenFilter",urlPatterns = "/*")
public class WmTokenFilter extends GenericFilter {

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        String id = request.getHeader("id");
        if (StringUtils.isNotBlank(id)){
            WmUser wmUser = new WmUser();
            wmUser.setId(Integer.valueOf(id));
            WmThreadLocalUtils.setUser(wmUser);
        }
        filterChain.doFilter(request,response);



    }
}
